package com.alibaba.jingn.service.impl;

import cn.alibaba.open.param.*;
import com.alibaba.jingn.constant.BusinessConstant;
import com.alibaba.jingn.model.SonUser;
import com.alibaba.jingn.service.MaterialService;
import com.alibaba.jingn.util.StringUtils;
import com.alibaba.ocean.rawsdk.ApiExecutor;
import com.alibaba.ocean.rawsdk.common.SDKResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;

/**
 * 物资系统
 * @author ZhangXuHao
 * @date 2021/04/15  16:04
 */
@Service
public class MaterialServiceimpl implements MaterialService
{

    //设置appkey和密钥(seckey)
    private  ApiExecutor apiExecutor = new ApiExecutor(BusinessConstant.appKey,BusinessConstant.appSecret);


    @Override
    public Map<String, Object> detailsParam(String pricingChartId) {
        ComAlibabaCaigouCoopapiPricingchartServicePricingChartCoopServiceGetPricingChartByIdParam caccpsp = new ComAlibabaCaigouCoopapiPricingchartServicePricingChartCoopServiceGetPricingChartByIdParam();
        caccpsp.setPricingChartId(Long.valueOf(pricingChartId));
        Map returnmap = new HashMap<String,Object>();
        //设置appkey和密钥(seckey)
        ApiExecutor apiExecutor = new ApiExecutor(BusinessConstant.appKey,BusinessConstant.appSecret);
        //调用API并获取返回结果
        SDKResult<ComAlibabaCaigouCoopapiPricingchartServicePricingChartCoopServiceGetPricingChartByIdResult> result = apiExecutor.execute(caccpsp,BusinessConstant.accessToken);
        if (StringUtils.isNull(result.getErrorCode())){
            ComAlibabaCaigouCoopapiPricingchartServicePricingChartCoopServiceGetPricingChartByIdResult details=result.getResult();
            returnmap.put("msg",0);
            returnmap.put("success",details.getSuccess());
            if(details.getSuccess()){
                returnmap.put("result",details);
            }else{
                returnmap.put("message",details.getMessage());
            }
        } else {
            returnmap.put("msg",1);
            returnmap.put("news",result.getErrorMessage());
        }
        return returnmap;
    }

    @Override
    public Map<String, Object> insrtSonUser(CaigouApiAccountCreateSubAccountParam record) {
        Map returnmap = new HashMap<String,Object>();
        //调用API并获取返回结果
        SDKResult<CaigouApiAccountCreateSubAccountResult> sdkresult = apiExecutor.execute(record,BusinessConstant.accessToken);
        if (StringUtils.isNull(sdkresult.getErrorCode())){
            CaigouApiAccountCreateSubAccountResult result = sdkresult.getResult();
            returnmap.put("success",true);
            SonUser su = new SonUser();
            su.setLoginId(result.getLoginId());
            su.setEmployeeId(result.getEmployeeId());
            su.setSubUserId(result.getSubUserId().toString());
            returnmap.put("result",su);
        } else {
            returnmap.put("success",false);
            returnmap.put("code",sdkresult.getErrorCode());
            returnmap.put("message",sdkresult.getErrorMessage());
        }
        return returnmap;
    }

    @Override
    public Map<String, Object> inseterp(List<Map<String,Object>>  list) {
        Map returnmap = new HashMap<String,Object>();
        AlibabaCaigouCategoryAddUserErpCategoryParam record = new AlibabaCaigouCategoryAddUserErpCategoryParam();
        record.setCategoryList(list);
        //调用API并获取返回结果
        SDKResult<AlibabaCaigouCategoryAddUserErpCategoryResult> sdkresult = apiExecutor.execute(record,BusinessConstant.accessToken);
        if (StringUtils.isNull(sdkresult.getErrorCode())){
            AlibabaCaigouCategoryAddUserErpCategoryResult result = sdkresult.getResult();
            returnmap.put("success",true);
            returnmap.put("result",result);
        } else {
            returnmap.put("success",false);
            returnmap.put("code",sdkresult.getErrorCode());
            returnmap.put("message",sdkresult.getErrorMessage());
        }
        return returnmap;
    }

    @Override
    public Map<String, Object> updateerp(AlibabaCaigouProductAddOrUpdateErpProductCoopParam[] alibabaCaigouProductAddOrUpdateErpProductCoopParams) {
        Map returnmap = new HashMap<String,Object>();
        AlibabaCaigouProductBatchAddOrUpdateErpProductsParam record = new AlibabaCaigouProductBatchAddOrUpdateErpProductsParam();
        record.setCoopParams(alibabaCaigouProductAddOrUpdateErpProductCoopParams);
        SDKResult<AlibabaCaigouProductBatchAddOrUpdateErpProductsResult> sdkresult = apiExecutor.execute(record,BusinessConstant.accessToken);
        if (StringUtils.isNull(sdkresult.getErrorCode())){
            AlibabaCaigouProductBatchAddOrUpdateErpProductsResult result = sdkresult.getResult();
            if(result.getSuccess()){
                returnmap.put("success",true);
                returnmap.put("data",result.getData());
            }else {
                returnmap.put("success",false);
                returnmap.put("code",result.getResultCode());
                returnmap.put("message",result.getMessage());
            }
        } else {
            returnmap.put("success",false);
            returnmap.put("code",sdkresult.getErrorCode());
            returnmap.put("message",sdkresult.getErrorMessage());
        }
        return returnmap;
    }

    /**
     * 询价单
     * @param record
     * @return
     */
    @Override
    public  Map<String,Object> inquiryparam(CaigouApiBuyofferPostBuyofferParam record){
        Map returnmap = new HashMap<String,Object>();
        //调用API并获取返回结果
        SDKResult<CaigouApiBuyofferPostBuyofferResult> result = apiExecutor.execute(record,BusinessConstant.accessToken);
        if (StringUtils.isNull(result.getErrorCode())){
            String orderId = result.getResult().getId().toString();
            returnmap.put("success",true);
            returnmap.put("orderId",orderId);
        } else {
            returnmap.put("success",false);
            returnmap.put("code",result.getErrorCode());
            returnmap.put("message",result.getErrorMessage());
        }
        return returnmap;
    }

    @Override
    public Map<String, Object> comparisonApproval(AlibabaCaigouCoopapiPricingchartParamPricingChartPostParam record) {
        Map returnmap = new HashMap<String,Object>();
        ComAlibabaCaigouCoopapiPricingchartServicePricingChartCoopServicePostPricingChartParam comparison = new ComAlibabaCaigouCoopapiPricingchartServicePricingChartCoopServicePostPricingChartParam();
        comparison.setPricingChartParam(record);
        //调用API并获取返回结果
        SDKResult<ComAlibabaCaigouCoopapiPricingchartServicePricingChartCoopServicePostPricingChartResult> sdkresult = apiExecutor.execute(comparison,BusinessConstant.accessToken);
        if (StringUtils.isNull(sdkresult.getErrorCode())){
            ComAlibabaCaigouCoopapiPricingchartServicePricingChartCoopServicePostPricingChartResult result = sdkresult.getResult();
            if(result.getSuccess()){
                returnmap.put("success",true);
                returnmap.put("data",result.getData());
            }else {
                returnmap.put("success",false);
                returnmap.put("code",result.getResultCode());
                returnmap.put("message",result.getMessage());
            }
        } else {
            returnmap.put("success",false);
            returnmap.put("code",sdkresult.getErrorCode());
            returnmap.put("message",sdkresult.getErrorMessage());
        }
        return returnmap;
    }

    @Override
    public Map<String, Object> queryOrder(Long procurementOrderId) {
        Map returnmap = new HashMap<String,Object>();
        AlibabaCaigouProcurementOrderQueryProcurementOrderDetailParam record = new AlibabaCaigouProcurementOrderQueryProcurementOrderDetailParam();
        record.setProcurementOrderId(procurementOrderId);
        //调用API并获取返回结果
        SDKResult<AlibabaCaigouProcurementOrderQueryProcurementOrderDetailResult> sdkresult = apiExecutor.execute(record,BusinessConstant.accessToken);
        if (StringUtils.isNull(sdkresult.getErrorCode())){
            AlibabaCaigouProcurementOrderQueryProcurementOrderDetailResult result = sdkresult.getResult();
            if(result.getSuccess()){
                returnmap.put("success",true);
                returnmap.put("data",result.getData());
            }else {
                returnmap.put("success",false);
                returnmap.put("code",result.getResultCode());
                returnmap.put("message",result.getMessage());
            }
        } else {
            returnmap.put("success",false);
            returnmap.put("code",sdkresult.getErrorCode());
            returnmap.put("message",sdkresult.getErrorMessage());
        }
        return returnmap;
    }

    @Override
    public Map<String, Object> querySupplier(String[] outCodes) {
        CaigouSupplierOceanGetSupplierMemberIdParam asosp = new CaigouSupplierOceanGetSupplierMemberIdParam();
        AlibabaChinaCaigouSupplierApiParamSplOceanSupplierMemberIdOceanGetParam amp =new AlibabaChinaCaigouSupplierApiParamSplOceanSupplierMemberIdOceanGetParam();
        amp.setOutCodes(outCodes);
        amp.setParamType("outCode");
        asosp.setSupplierMemberIdGetParam(amp);

        Map returnmap = new HashMap<String,Object>();

        SDKResult<CaigouSupplierOceanGetSupplierMemberIdResult> sdkresult = apiExecutor.execute(asosp,BusinessConstant.accessToken);
        if (StringUtils.isNull(sdkresult.getErrorCode())){
            CaigouSupplierOceanGetSupplierMemberIdResult result = sdkresult.getResult();
            if(result.getSuccess()){
                returnmap.put("success",true);
                returnmap.put("data",result.getData());
            }else {
                returnmap.put("success",false);
                returnmap.put("code",result.getErrorCode());
                returnmap.put("message",result.getErrorMessage());
            }
        } else {
            returnmap.put("success",false);
            returnmap.put("code",sdkresult.getErrorCode());
            returnmap.put("message",sdkresult.getErrorMessage());
        }
        return returnmap;
    }

}